package edu.calpoly.android.lab2;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import edu.calpoly.android.lab2.Joke;

public class SimpleJokeList extends Activity {

	/** Contains the list Jokes the Activity will present to the user **/
	protected ArrayList<Joke> m_arrJokeList;

	/**
	 * LinearLayout used for maintaining a list of Views that each display Jokes
	 **/
	protected LinearLayout m_vwJokeLayout;

	/**
	 * EditText used for entering text for a new Joke to be added to
	 * m_arrJokeList.
	 **/
	protected EditText m_vwJokeEditText;

	/**
	 * Button used for creating and adding a new Joke to m_arrJokeList using the
	 * text entered in m_vwJokeEditText.
	 **/
	protected Button m_vwJokeButton;
	
	/**
	 * Background Color values used for alternating between light and dark rows
	 * of Jokes.  
	 */
	protected int m_nDarkColor;
	protected int m_nLightColor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		m_arrJokeList = new ArrayList<Joke>();
		initLayout();
		initAddJokeListeners();
		Resources resources = this.getResources();
		String[] jokes = resources.getStringArray (R.array.jokeList);
		for (String joke : jokes) {
			addJoke(joke);
		}

	}
	
	/**
	 * Method used to encapsulate the code that initializes and sets the Layout
	 * for this Activity. 
	 */
	protected void initLayout() {
		LinearLayout.LayoutParams fit_contents = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		LinearLayout.LayoutParams fit_parent = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		m_vwJokeLayout = new LinearLayout(this);
		m_vwJokeLayout.setOrientation(LinearLayout.VERTICAL);
		ScrollView bodyLayout = new ScrollView(this);
		bodyLayout.addView(m_vwJokeLayout);
		m_vwJokeButton = new Button(this);
		m_vwJokeButton.setText("Add Joke");
		m_vwJokeEditText = new EditText(this);
		m_vwJokeEditText.setLayoutParams(fit_parent);
		LinearLayout headLayout = new LinearLayout(this);
		headLayout.setOrientation(LinearLayout.HORIZONTAL);
		headLayout.addView(m_vwJokeButton);
		headLayout.addView(m_vwJokeEditText);
		headLayout.setLayoutParams(fit_parent);
		LinearLayout mainLayout = new LinearLayout(this);
		mainLayout.setOrientation(LinearLayout.VERTICAL);
		mainLayout.addView(headLayout);
		mainLayout.addView(bodyLayout);
		setContentView(mainLayout);
	}
	
	/**
	 * Method used to encapsulate the code that initializes and sets the Event
	 * Listeners which will respond to requests to "Add" a new Joke to the 
	 * list. 
	 */
	protected void initAddJokeListeners() {
		m_vwJokeButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String addJokeText = m_vwJokeEditText.getText().toString();
				m_vwJokeEditText.setText("");
				addJoke(addJokeText);
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);
			}
		});
		m_vwJokeEditText.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_DPAD_CENTER:
				case KeyEvent.KEYCODE_ENTER:
					String addJokeText = m_vwJokeEditText.getText().toString();
					m_vwJokeEditText.setText("");
					addJoke(addJokeText);
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);				
					break;

				default:
					break;
				}
				return false;
			}
		});
	}
	private void updateBackground() {
		Resources resources = this.getResources();
		int count = m_vwJokeLayout.getChildCount();
		for (int i = 0; i < count; i++) {
			TextView textView = (TextView) m_vwJokeLayout.getChildAt(i);
			if (i % 2 == 1) {	
				textView.setBackgroundColor(resources.getColor(R.color.DARK));
			} else {
				textView.setBackgroundColor(resources.getColor(R.color.LIGHT));
			}
		}
	}
	/**
	 * Method used for encapsulating the logic necessary to properly initialize
	 * a new joke, add it to m_arrJokeList, and display it on screen.
	 * 
	 * @param strJoke
	 *            A string containing the text of the Joke to add.
	 */
	protected void addJoke(String strJoke) {
		TextView textView = new TextView(this);
		textView.setText(strJoke);
		textView.setTextSize(16);
		m_vwJokeLayout.addView(textView);
		m_arrJokeList.add(new Joke(strJoke));
		updateBackground();
	}
}